import 'package:flutter/material.dart';

/// PPRichText 富文本
///
/// {@tool sample}
///
/// ```dart
/// PPRichText(
///             children: [
///               TextSpan(text: ' 我是Container -> ', style: defaultStyle),
///               Container(
///                 width: 50,
///                 height: 50,
///                 color: Colors.yellow,
///               ),
///               TextSpan(text: ' 我是Image -> ', style: defaultStyle),
///               Image.asset(
///                 "assets/common/btn_checkmark_on.png",
///                 width: 20,
///                 height: 20,
///               ),
///               TextSpan(text: ' 我已同意 ', style: defaultStyle),
///               TextSpan(text: '服务条款', style: linkStyle, recognizer: recognizer),
///               TextSpan(text: ' 和 ', style: defaultStyle),
///               TextSpan(text: '隐私政策', style: linkStyle, recognizer: recognizer),
///               TextSpan(text: ' 。Thank you', style: defaultStyle),
///             ],
///           )
/// {@end-tool}
///
class PPRichText extends StatelessWidget {
  final List<Object>? children;
  final TextAlign? textAlign;
  const PPRichText({Key? key, required this.children, this.textAlign})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    var list = <InlineSpan>[];
    children?.forEach((element) {
      if (element is InlineSpan) {
        list.add(element);
      } else if (element is Widget) {
        list.add(WidgetSpan(child: element));
      }
    });

    var textSpan = TextSpan(children: [...list]);

    return Text.rich(textSpan, textAlign: textAlign);
  }
}
